package com.star.spring.deferred.service.thread.basic;


import com.star.spring.deferred.utils.CommonUtils;

/**
 * @description: 打断测试
 * @author: star
 * @create: 2024-12-29 22:47
 **/

public class InterruptDemo {

    public static void main(String[] args) {
        // 打断阻塞的线程
        interruptedBlockThread();


        // 打断正常运行搞得线程
//        interruptedRunThread();
    }

    private static void interruptedRunThread() {

        Thread thread = new Thread(() -> {
            while (true) {
                CommonUtils.log("线程正常运行中");
                if (Thread.currentThread().isInterrupted()) {
                    CommonUtils.log("线程被打断了");
                    break;
                }
            }
        });

        thread.start();

        // 休眠1s让子线程跑起来
        CommonUtils.sleep(1);

        thread.interrupt();
        CommonUtils.log("线程的打断状态status为："+thread.isInterrupted());
    }

    private static void interruptedBlockThread() {

        Thread thread = new Thread(() -> {
            CommonUtils.sleep(2);

        });
        thread.start();
        CommonUtils.sleep(1);
        thread.interrupt();
        CommonUtils.log("线程的打断状态status为："+thread.isInterrupted());

    }
}
